package com.wuyan.locationdog.ui.my

import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.SuccessBean
import com.arcsoft.commonbase.bean.WorkOrderBeanDetailContent
import com.arcsoft.commonbase.ext.ImageOptions
import com.arcsoft.commonbase.ext.btnEnable
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.fileToPart
import com.arcsoft.commonbase.ext.loadImage
import com.arcsoft.commonbase.ext.textStringTrim
import com.blankj.utilcode.util.ActivityUtils
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.FeedbackDetailAdapter
import com.wuyan.locationdog.databinding.MyFeedbackDetailActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.ui.device.AddSuccess2Activity
import com.wuyan.locationdog.util.imageselecter.GlideEngine
import com.wuyan.locationdog.util.imageselecter.ImageCompressEngine
import com.wuyan.locationdog.viewmodel.MyViewModel
import okhttp3.RequestBody
import java.io.File


class MyFeedbackDetailActivity : BaseBindingActivity<MyFeedbackDetailActivityBinding>() {

    private var mWorkOrderBeanDetailContent: WorkOrderBeanDetailContent? = null
    private var fileUrl: String? = null

    private val mViewModel: MyViewModel by viewModels()

    private val mAdapter by lazy { FeedbackDetailAdapter(R.layout.item_feedback_detail, null) }

    override fun initData() {
        val workOrderId = intent.getStringExtra("workOrderId")
        setTitleText("反馈详情")


        binding.apply {


            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无数据"))


                    setOnItemClickListener { adapter, view, position ->

                    }
                }
            }
            btnAlbum.setOnClickListener {
                PictureSelector.create(mActivity)
                    .openGallery(SelectMimeType.ofImage())
                    .setImageEngine(GlideEngine())
//                    .setCropEngine(ImageCropEngine())
                    .setCompressEngine(ImageCompressEngine())
                    .setMaxSelectNum(1)
                    .forResult(object : OnResultCallbackListener<LocalMedia> {
                        override fun onResult(result: ArrayList<LocalMedia>) {
                            val file = File(result[0].availablePath)
                            btnAlbum.loadImage(result[0].availablePath, ImageOptions().apply {

                            })
                            val map = HashMap<String, RequestBody>()
                            mViewModel.fileUpload(map, fileToPart(file))
                        }

                        override fun onCancel() {}
                    })
            }
            etDesc.addTextChangedListener {
                btnEnable(btnSure, etDesc)
            }
            btnSure.setOnClickListener {
                val map = HashMap<String, Any>()
                map["content"] = etDesc.textStringTrim()
                map["workOrderId"] = mWorkOrderBeanDetailContent?.workOrderId.toString()
                fileUrl?.let {
                    map["img"] = fileUrl.toString()
                }
                mViewModel.submitWorkOrder(map)
            }

        }


        val map = HashMap<String, Any>()
        map["workOrderId"] = workOrderId.toString()
        mViewModel.workOrderDetail(map)
    }

    override fun observe() {
        super.observe()

        mViewModel.workOrderBeanDetail.observe(this) {
            mWorkOrderBeanDetailContent = it.content
            binding.apply {
                title.text = it.content.title
                createTime.text = it.content.workOrderRecordList.first().createTime
            }

            mAdapter.setNewInstance(it.content.workOrderRecordList.toMutableList())
            mAdapter.imei = it.content.imei
        }

        mViewModel.fileUploadBean.observe(this) {
            fileUrl = it.content.fileUrl

        }

        mViewModel.anyBean.observe(this) {
            DeviceHelp.successBean = SuccessBean("提交成功","反馈已提交成功！您可以在我的反馈中查看反馈状态，我们会尽快处理您的问题。谢谢您的支持与配合！")

            ActivityUtils.startActivity(AddSuccess2Activity::class.java)

        }
    }

}